<?php
/**
 *
 */
class brand extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('M_brand');
    $this->load->model('M_summary');
    $this->load->helper('url');
    $this->load->helper('form');
    // $this->load->library('form_validation');
    $this->load->library('session');
    if($this->session->userdata('username')==null){
      redirect('/login');
    }
  }
  public function index(){
    $this->load->library('pagination');
    $page = 0;
    $offset = 0;
    $per_page = 10;
    if(!empty($_GET['per_page'])){
      $page = $_GET['per_page'];
      $offset = ($page-1)*$per_page;
    }
    $result = $this->M_brand->getBrandlist($per_page,$offset);
    if(!empty($_GET['search'])){
      $result = $this->M_brand->getBrand($_GET['search'],$per_page,$offset);
    }
    $config['base_url']=site_url('brand');
    $config['total_rows'] = $result['total'];
    $config['per_page'] = $per_page;
    $this->pagination->initialize($config);
    $data['brandlist'] = $result['brandlist'];
    $data['pager'] = $this->pagination->create_links();
    $this->load->view('brand',$data);
  }
  public function addBrand(){
    if(!empty($_POST['bname'])){
      echo $this->M_brand->addBrand($_POST['bname']);
    }
  }
  public function updateBrand(){
    if(!empty($_POST['bname']) && !empty($_POST['bid'])){
      print_r($this->M_brand->updateBrand($_POST['bid'],$_POST['bname']));
    }
  }
  public function deleteBrand(){
    if(!empty($_POST['bid'])){
      echo $this->M_brand->deleteBrand($_POST['bid']);
    }
  }
  public function getallbrand(){
    $page = 1;
    $offset = 0;
    $per_page = 10000;
    if(!empty($_GET['per_page'])){
      $page = $_GET['per_page'];
      $offset = ($page-1)*$per_page;
    }
    $result = $this->M_brand->getallbrand($per_page,$offset);
    $result['page']=$page;
    $result['total']%$per_page==0?$result['total']=(int)($result['total']/$per_page):$result['total']=(int)($result['total']/$per_page+1);
    print_r(json_encode($result));
    // $result = $this->M_brand->getallbrand();
    // print_r(json_encode($result));
  }
  public function getallbrandselect(){
    $result = $this->M_brand->getallbrandselect();
    print_r(json_encode($result));
  }
  public function tecountexcel(){
    $bid = $_GET['bid'];
    $month = '';
    $year = '';
    $count = 1000;
    $offset = 0;
    $countm=2;
    $countk=2;
    $adddaym=0;
    $adddayy=0;
    if(!empty($_GET['month']) && !empty($_GET['year'])){
      $month = $_GET['month'];
      $year = $_GET['year'];
    }
    $result = $this->M_summary->gettecountbrand($bid,$month,$year,$count,$offset);
    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename='终端业绩.csv'");
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0'); header('Pragma:public');
    $str = "日期,店家名称,终端业绩\n";
    echo $str;
    while ($result['row']>0) {
      $result = $this->M_summary->gettecountbrand($bid,$month,$year,$count,$offset);
      foreach ($result['result'] as $shop) {
        $month = explode("-", $shop['addday']);
        if($adddaym==0)$adddaym=$month[1];
        if($adddayy==0)$adddayy=$month[0];
        if($adddaym!=$month[1] || $adddayy!=$month[0]){
          echo $adddaym."(月)合计, ,=SUM(C".$countk.":C".--$countm.")\n";
          $countm+=1;
          $countk=$countm;
          $adddaym=$month[1];
          $adddayy=$month[0];
        }
        $str = $shop['addday'].",".$shop['shopname'].",".$shop['tecount']."\n";
        echo $str;
      }
      $offset +=$count;
    }
    if($countm!=2){
      echo $adddaym."(月)合计, ,=SUM(C".$countk.":C".--$countm.")\n";
    }
    return false;
  }
  public function tebrandexcel(){
    $bid = $_GET['bid'];
    $result = $this->M_summary->gettebrand($bid);
    $this->load->library('PHPExcel');
    $this->load->library('PHPExcel/IOFactory');
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle('summary');
    $title = array(
      array('编号', '终端客户姓名', '店家名称'),
      );
    $objPHPExcel->getActiveSheet() ->fromArray( $title, NULL, 'A1' );
    $i = 2;
    foreach ($result as $shop) {
      $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $shop['teid']);
      $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $shop['tename']);
      $objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $shop['shopname']);
      $i++;
    }
    // 保存excel—2007格式
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式
    // 直接输出到浏览器
    // $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    header("Content-Type:application/force-download");
    header("Content-Type:application/vnd.ms-execl");
    header("Content-Type:application/octet-stream");
    header("Content-Type:application/download");;
    header('Content-Disposition:attachment;filename="终端明细.xls"');
    header("Content-Transfer-Encoding:binary");
    $objWriter->save('php://output');
  }
  public function getshopbrand(){
    if(!empty($_GET['shopid'])){
      $result = $this->M_brand->getshopbrand($_GET['shopid']);
      print_r(json_encode($result));
    }else{
      echo "false";
    }
  }
}
